from openai import OpenAI

client = OpenAI(
    api_key="sk-aAhAYFgW8RhLCPGvfVCTJeLbUuKqO5zkfN9YDICnbldaHGPh",
    base_url="https://api.chatanywhere.tech/v1"
)


# 非流式响应
def gpt_35_api(messages: list):
    completion = client.chat.completions.create(model="gpt-3.5-turbo", messages=messages, temperature=0, top_p=1,
                                                max_tokens=9999)
    return completion.choices[0].message.content


# 流式响应
def gpt_35_api_stream(messages: list):
    stream = client.chat.completions.create(
        model='gpt-3.5-turbo',
        messages=messages,
        stream=True,
        temperature=0.1,
        top_p=1
    )
    for chunk in stream:
        if chunk.choices[0].delta.content is not None:
            yield chunk.choices[0].delta.content
